﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LoginServer
{
    public class Room
    {
        public RoomInfo info { get; set; }
        public ManagerTable Manager_Table { get; set; }
        public ManagerPlayer Manager_Player { get; set; } 

        public Room(RoomInfo roominfo)
        {
            this.info = roominfo;
        }
        internal void LoadTable(GetDB AccessDB)
        {
            Manager_Table = new ManagerTable(AccessDB, info.RoomID);
        }
        public void LoadPlayer(GetDB AccessDB)
        {
            Manager_Player = new ManagerPlayer(AccessDB, info.RoomID);
            info.CountUser = Manager_Player.Players.Count;
        }
        public override string ToString()
        {

            return info.ToString();
        }
        public string Str_JoinRoom()
        {
            return string.Format("{0}|{1}", info.RoomID, info.CountUser);
        }
        public string Str_OutRoom()
        {
            return string.Format("{0}|{1}", info.RoomID, info.CountUser);
        }
        public string Str_CreateRoom()
        {
            return string.Format("{0}|{1}|{2}|{3}", info.RoomID, info.Name, info.Level, info.CountUser);
        }



        internal void OutRoom(int UserID)
        {
            info.CountUser -= 1;
            Manager_Player.RemovePlayer(UserID);
        }

        internal void OutTable(int TableID, int UserID)
        {
            int playerID2 = Manager_Table.GetTable(TableID).info.PlayerID2;
            if (Manager_Table.GetTable(TableID).info.PlayerID2 == UserID)
            {
                Manager_Table.RemovePlayerInTable(TableID, playerID2);
                Manager_Player.PlayerInTable.Remove(playerID2);
            }
            else
            {
                Manager_Table.RemovePlayerInTable(TableID, UserID);
                Manager_Player.PlayerInTable.Remove(UserID);
                Manager_Table.RemovePlayerInTable(TableID, playerID2);
                Manager_Player.PlayerInTable.Remove(playerID2);
            }
        }

        internal void RemoveTable(Table table)
        {
            Manager_Table.RemoveTable(table);
        }

        internal bool CheckUserInRoom(int UserID)
        {
            if (Manager_Player.Players.Contains(UserID))
                return true;
            Console.WriteLine("User Not in Room");
            return false;
        }
        internal bool CheckUserInTable(int UserCreatID)
        {
            if (Manager_Player.PlayerInTable.Contains(UserCreatID))
                return true;
            Console.WriteLine("user not in table");
            return false;
        }

        internal bool CheckUserOutTable(int UserCreatID)
        {
            if (!Manager_Player.PlayerInTable.Contains(UserCreatID))
                return true;
            Console.WriteLine("user in table");
            return false;
        }

        internal bool CheckUserOutRoom(int UserID)
        {
            if (!Manager_Player.Players.Contains(UserID))
                return true;
            Console.WriteLine("User in Room");
            return false;
        }

        internal bool CheckTableInRoom(int TableID)
        {
            if (Manager_Table.CheckTableID(TableID))
                return true;
            Console.WriteLine("Table Not in Room");
            return false;
        }

        internal void CreateTableOK(int UserCreateID, Table table)
        {
            Manager_Player.PlayerInTable.Add(UserCreateID);//manager player
            Manager_Table.AddTable(table);//manager table
        }

        internal void JoinTableOK(int TableJoinID, int UserJoinID)
        {
            Manager_Table.AddPlayerInTable(TableJoinID, UserJoinID);//manager table
            Manager_Player.PlayerInTable.Add(UserJoinID);//manager player
        }

        internal void JoinRoomOK(int UserJoinID)
        {
            Manager_Player.Players.Add(UserJoinID);//manager room
            info.CountUser += 1;
        }
    }
}
